//
//  MessageSetViewController.m
//  安好
//
//  Created by 冯凯 on 14-11-24.
//  Copyright (c) 2014年 lianchuangbrother. All rights reserved.
//

#import "MessageSetViewController.h"
#import "setSecondCell.h"

@interface MessageSetViewController ()

@end

@implementation MessageSetViewController
/**
 *  yes的时候，返回的时候请求数据，叫消息设置的内容传给服务器
 *
 *  @param animated 无
 */
- (void)viewWillAppear:(BOOL)animated
{
    NSUserDefaults * user = [NSUserDefaults standardUserDefaults];
    NSString * uid = [[UserManager shareInstance]getUserInfo].uid;
    NSString *pushmessAge = [NSString stringWithFormat:@"PushmessAge%@",uid];
    NSString *pushmess= [NSString stringWithFormat:@"Pushmess%@",uid];
    [user setObject:@"PushmessAge" forKey:pushmessAge];
    [user setBool:YES forKey:pushmess];

}
/**
 *  移除Key  返回的时候就不会请求消息通知
 *
 *  @param animated 无
 */
- (void)viewWillDisappear:(BOOL)animated
{
    NSUserDefaults * user = [NSUserDefaults standardUserDefaults];
    NSString * uid = [[UserManager shareInstance]getUserInfo].uid;
    NSString *pushmessAge = [NSString stringWithFormat:@"PushmessAge%@",uid];
    [user removeObjectForKey:pushmessAge];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    [self remoteNotificationTyep];
    /**
     *  判断通知状态，加载数据
     */
    if (self.isOpenMessAgePush == YES) {
        [self loadWithData];
    }else{
        
    }
    //加载导航条
    [self makeNavBar];
    //加载数据
    [self loadData];
    //加载tableView
    [self createTableView];
    
}
#pragma - mark  请求消息的数据
/**
 *  请求消息设置的数据
 */
- (void)loadWithData
{
    NSString * postURL = [NSString stringWithFormat:@"%@setting/getmessage/?platform=%@?version=%@?people=2",kBaseURL,kPlatform,kVersion];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:postURL parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSNumber * statusNum = [responseObject objectForKey:@"status"];
        NSNumberFormatter* numberFormatter = [[NSNumberFormatter alloc] init];
        NSString * statusString = [numberFormatter stringFromNumber:statusNum];
        if ([statusString isEqualToString:@"1"]) {
            self.openDic=[responseObject valueForKey:@"data"];
            [self.systemTableView reloadData];
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
       
        
    }];

}
#pragma - mark 创建显示数据
/**
 *  创建数据
 */
-(void)loadData
{
    self.openArray = @[@"接受新消息通知"];
    self.newsArray = @[@"接收新消息提醒",@"声音",@"震动",@"显示消息详细内容"];
    self.activeArray = @[@"接收活动信息"];
    self.nightArray = @[@"夜间防打扰设置"];
}
#pragma mark - 配置导航条
-(void)makeNavBar
{
    self.navigationItem.title = @"消息设置";
    // 返回按钮图片
    UIBarButtonItem *backBtnImg = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"公用_返回.png"] style:UIBarButtonItemStylePlain target:self action:@selector(backMoreVC)];
    backBtnImg.tintColor = [UIColor whiteColor];
    self.navigationItem.leftBarButtonItem = backBtnImg;
}
#pragma mark - 创建tableView设置代理
-(void)createTableView
{
    self.systemTableView = [[UITableView alloc] initWithFrame:CGRectMake(0,0, kScreenWidth, kScreenHeight - iOS7TopMargin) style:UITableViewStyleGrouped];
    self.systemTableView.delegate = self;
    self.systemTableView.dataSource = self;
    //隐藏横向和竖向的滑条
    self.systemTableView.showsHorizontalScrollIndicator = NO;
    self.systemTableView.showsVerticalScrollIndicator = NO;
    self.systemTableView.backgroundColor = [ClassMethodsViewController colorWithHexString:BACK_BACKGROUNDCOLOR_16];
    [self.view addSubview:self.systemTableView];
}
#pragma - mark  判断通知状态
/**
 *  判断通知状态
 */
- (void)remoteNotificationTyep
{
//    UIRemoteNotificationType type = [[UIApplication sharedApplication]enabledRemoteNotificationTypes];
//    NSString * typeString = [NSString stringWithFormat:@"%u",type];
//    if ([typeString isEqualToString:@"0"]) {
//        self.isOpenMessAgePush = NO;
//    }else{
//        self.isOpenMessAgePush = YES;
//    } 
    
    if(IOS8_OR_LATER){
        BOOL isRemoteNotify = [UIApplication sharedApplication].isRegisteredForRemoteNotifications;
        if (isRemoteNotify){
            self.isOpenMessAgePush = YES;
 
        }else{

            self.isOpenMessAgePush = NO;
        }
    }
    else{
        UIRemoteNotificationType type = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
        int typeBadge = (type & UIRemoteNotificationTypeBadge);
        int typeSound = (type & UIRemoteNotificationTypeSound);
        int typeAlert = (type & UIRemoteNotificationTypeAlert);
        int typenone = (type & UIRemoteNotificationTypeNone);
        if (typeAlert == 0 && typeBadge == 0 && typeSound == 0 && typenone == 0) {
            
            self.isOpenMessAgePush = NO;
        
        }else{
            self.isOpenMessAgePush = YES;
            
        }
    }
    
}
#pragma - mark 返回
/**
 *  返回
 */
- (void)backMoreVC
{
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma - mark  tableview
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if (self.isOpenMessAgePush == YES) {
    return 4;
    }else{
    return 1;
    }
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (self.isOpenMessAgePush == YES) {
        if (section == 0) {
            return self.openArray.count;
        }else if (section == 1){
            return self.newsArray.count;
        }else if (section == 2){
            return self.activeArray.count;
        }else{
            return self.nightArray.count;
        }
    }else{
        return self.openArray.count;
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (kScreenWidth == 414){
        return 56;
        
    }else{
        
        return 46;
    }
}

//  下面两个数据源方法是必须实现的
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        static NSString * identifier = @"myCell";
        //  检测、查询是否有闲置的单元格
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (cell == nil){
            cell =  [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        }
        //  返回每行的单元格对象
        if (indexPath.section == 0) {
            cell.textLabel.text = self.openArray[indexPath.row];
            cell.textLabel.font = [UIFont systemFontOfSize:15];
            UILabel * ophenLabel = [[UILabel alloc]initWithFrame:CGRectMake(kScreenWidth - 80 - 15, 14, 80, 20)];
            if (kScreenWidth == 414) {
                ophenLabel.frame = CGRectMake(kScreenWidth - 80 - 15, 19, 80, 20);
            }else{
                
            }
            ophenLabel.textAlignment = NSTextAlignmentRight;
            if (self.isOpenMessAgePush == YES) {
                ophenLabel.text = @"已开启";
            }else{
                ophenLabel.text = @"已关闭";
            }
            ophenLabel.font = [UIFont systemFontOfSize:LABEL_FONT_SIZE_CONTENT_13];
            ophenLabel.textColor = [UIColor colorWithRed:31.0/255 green:33.0/255 blue:36.0/255 alpha:1];
            [cell addSubview:ophenLabel];
        }
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        return cell;
        
        }else {
        setSecondCell *cell = [tableView dequeueReusableCellWithIdentifier:@"second"];
        if (cell == nil) {
            if (kScreenWidth == 414){
                cell = [[[NSBundle mainBundle] loadNibNamed:@"setSecondCell" owner:self options:nil] lastObject];
                
            }else{
                cell = [[[NSBundle mainBundle] loadNibNamed:@"setSecondCell" owner:self options:nil] firstObject];

            }
        }
         if (indexPath.section == 1){
            if (indexPath.row ==0) {
            NSString * soundString = [NSString stringWithFormat:@"%@",[self.openDic objectForKey:@"remind"]];
            if ([soundString isEqualToString:@"0"]) {
                cell.isSwitch.on = NO;
            }
            }else if (indexPath.row == 3){
                  NSString * avoidString = [NSString stringWithFormat:@"%@",[self.openDic objectForKey:@"showdetail"]];
                if ([avoidString isEqualToString:@"0"]) {
                    cell.isSwitch.on = NO;
                }
            }
        cell.isSwitch.tag = 10+indexPath.row;
        [cell.isSwitch addTarget:self action:@selector(isSwitch:) forControlEvents:UIControlEventTouchUpInside];
        cell.titleLable.text = self.newsArray[indexPath.row];
    }else if (indexPath.section == 2){
        NSString * avoidString = [NSString stringWithFormat:@"%@",[self.openDic objectForKey:@"avoid"]];
        cell.isSwitch.on = YES;
        if ([avoidString isEqualToString:@"0"]) {
            cell.isSwitch.on = NO;
        }
        cell.isSwitch.tag = 20+indexPath.row;
        [cell.isSwitch addTarget:self action:@selector(isSwitch:) forControlEvents:UIControlEventTouchUpInside];
        cell.titleLable.text = self.activeArray[indexPath.row];
        
    }else{
        cell.isSwitch.tag = 30+indexPath.row;
        [cell.isSwitch addTarget:self action:@selector(isSwitch:) forControlEvents:UIControlEventTouchUpInside];
        cell.titleLable.text = self.nightArray[indexPath.row];
    }
    return cell;
    }
}
//设置段头高
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section== 1) {
        return 46;
    }else{
    return 10;
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.1;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (section== 1) {
        UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 46)];
        view.backgroundColor = [UIColor clearColor];
        UILabel * sectionLabel = [[UILabel alloc]initWithFrame:CGRectMake(15, 13, kScreenWidth, 20)];
        sectionLabel.font = [UIFont systemFontOfSize:14];
        sectionLabel.text = @"请在iPhone的“设置” - “通知”功能中进行修改。";
        [view addSubview:sectionLabel];
        return view;
    }else{
        UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 10)];
        return view;
    }
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

}
/**
 *  判断状态
 *
 *  @param switchtag <#switchtag description#>
 */
- (void)isSwitch:(UISwitch *)switchtag
{
    if (switchtag.tag == 10) {
        if (switchtag.on == YES) {
            NSUserDefaults * user = [NSUserDefaults standardUserDefaults];
            NSString * uid = [[UserManager shareInstance]getUserInfo].uid;
            NSString * pushNew = [NSString stringWithFormat:@"pushNew%@",uid];
            [user setObject:@"1" forKey:pushNew];
        }else{
            NSUserDefaults * user = [NSUserDefaults standardUserDefaults];
            NSString * uid = [[UserManager shareInstance]getUserInfo].uid;
            NSString * pushNew = [NSString stringWithFormat:@"pushNew%@",uid];
            [user setObject:@"0" forKey:pushNew];
        }
    }else if (switchtag.tag == 11){
        if (switchtag.on == YES) {
            NSUserDefaults * user = [NSUserDefaults standardUserDefaults];
            NSString * uid = [[UserManager shareInstance]getUserInfo].uid;
            NSString * pushSound = [NSString stringWithFormat:@"pushSound%@",uid];
            [user setObject:@"1" forKey:pushSound];
        }else{
            NSUserDefaults * user = [NSUserDefaults standardUserDefaults];
            NSString * uid = [[UserManager shareInstance]getUserInfo].uid;
            NSString * pushSound = [NSString stringWithFormat:@"pushSound%@",uid];
            [user setObject:@"0" forKey:pushSound];
        }
    }else if (switchtag.tag == 12){
        if (switchtag.on == YES) {
            NSUserDefaults * user = [NSUserDefaults standardUserDefaults];
            NSString * uid = [[UserManager shareInstance]getUserInfo].uid;
            NSString * pushVibration = [NSString stringWithFormat:@"pushVibration%@",uid];
            [user setObject:@"1" forKey:pushVibration];
        }else{
            NSUserDefaults * user = [NSUserDefaults standardUserDefaults];
            NSString * uid = [[UserManager shareInstance]getUserInfo].uid;
            NSString * pushVibration = [NSString stringWithFormat:@"pushVibration%@",uid];
            [user setObject:@"0" forKey:pushVibration];
        }
    }else if (switchtag.tag == 13){
        if (switchtag.on == YES) {
            NSUserDefaults * user = [NSUserDefaults standardUserDefaults];
            NSString * uid = [[UserManager shareInstance]getUserInfo].uid;
            NSString * pushDetail = [NSString stringWithFormat:@"pushDetail%@",uid];
            [user setObject:@"1" forKey:pushDetail];
        }else{
            NSUserDefaults * user = [NSUserDefaults standardUserDefaults];
            NSString * uid = [[UserManager shareInstance]getUserInfo].uid;
            NSString * pushDetail = [NSString stringWithFormat:@"pushDetail%@",uid];
            [user setObject:@"0" forKey:pushDetail];
        }
    }else if (switchtag.tag == 20){
        if (switchtag.on == YES) {
            NSUserDefaults * user = [NSUserDefaults standardUserDefaults];
            NSString * uid = [[UserManager shareInstance]getUserInfo].uid;
            NSString * pushActivity = [NSString stringWithFormat:@"pushActivity%@",uid];
            [user setObject:@"1" forKey:pushActivity];
        }else{
            NSUserDefaults * user = [NSUserDefaults standardUserDefaults];
            NSString * uid = [[UserManager shareInstance]getUserInfo].uid;
            NSString * pushActivity = [NSString stringWithFormat:@"pushActivity%@",uid];
            [user setObject:@"0" forKey:pushActivity];
        }
    }else{
        if (switchtag.on == YES) {
            NSUserDefaults * user = [NSUserDefaults standardUserDefaults];
            NSString * uid = [[UserManager shareInstance]getUserInfo].uid;
            NSString * pushNight = [NSString stringWithFormat:@"pushNight%@",uid];
            [user setObject:@"1" forKey:pushNight];
        }else{
            NSUserDefaults * user = [NSUserDefaults standardUserDefaults];
            NSString * uid = [[UserManager shareInstance]getUserInfo].uid;
            NSString * pushNight = [NSString stringWithFormat:@"pushNew%@",uid];
            [user setObject:@"0" forKey:pushNight];
        }
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
